﻿using System;
using System.ComponentModel;
using System.Linq;

namespace Red.Core.Extensions
{
    public static class EnumExtensions
    {
        public static string GetDescription( this Enum e )
        {
            Type type = e.GetType();
            string value = e.ToString();

            DescriptionAttribute descriptionAttribute = type
              .GetField( value )
              .GetCustomAttributes( typeof( DescriptionAttribute ), false )
              .FirstOrDefault() as DescriptionAttribute;

            return descriptionAttribute != null
              ? descriptionAttribute.Description
              : value;
        }
    }
}
